package com.vcc.core.util;

import com.vcc.core.exception.ProgramException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    private static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";


    private static final SimpleDateFormat yyyyMMddHHmmssFormat = new SimpleDateFormat(yyyyMMddHHmmss);


    public static String format(Long s) {
        return yyyyMMddHHmmssFormat.format(new Date(s));
    }

    public static String format(Date s) {
        return yyyyMMddHHmmssFormat.format(s);
    }

    public static String format(String format, Date data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(data);
    }

    public static Date parse(String stringDate, String format) {
        SimpleDateFormat simpleDateFormat = format.equals(yyyyMMddHHmmss) ? yyyyMMddHHmmssFormat : new SimpleDateFormat(format);
        try {
            return simpleDateFormat.parse(stringDate);
        } catch (ParseException e) {
            throw new ProgramException("时间解析错误");
        }
    }

    public static Date parse(String stringDate) {
        try {
            return yyyyMMddHHmmssFormat.parse(stringDate);
        } catch (ParseException e) {
            throw new ProgramException("时间解析错误");
        }
    }

    public static Long getDistanceTimestamp(Date startDate, Date endDate) {
        return endDate.getTime() - startDate.getTime();
    }

    public static Date getDayBeginDate(String time, String format) {
        Date date = parse(time, format);
        return getDayBeginDate(date);
    }

    public static Date getDayEndDate(String time, String format) {
        Date date = parse(time, format);
        return getDayEndDate(date);
    }

    public static Date getDayBeginDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getDayEndDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    public static Boolean timeCalendar(Date nowTime, Date beginTime, Date endTime) {
        //设置当前时间
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        //设置开始时间
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);//开始时间
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);//结束时间
        //处于开始时间之后，和结束时间之前的判断
        return date.after(begin) && date.before(end);
    }

}
